{% extends 'base.html' %}

{% block title %}定时任务调试{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <h1 class="mb-4">定时任务调试</h1>
    
    <!-- Celery状态 -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="card-title">Celery状态</h5>
        </div>
        <div class="card-body">
            {% if celery_status %}
                <div class="alert alert-{{ celery_status.status|default:'info' }}">
                    <strong>状态:</strong> {{ celery_status.message }}
                </div>
                
                {% if celery_status.workers %}
                <div class="mb-3">
                    <strong>活动的Worker:</strong>
                    <ul>
                        {% for worker in celery_status.workers %}
                        <li>{{ worker }}</li>
                        {% endfor %}
                    </ul>
                </div>
                {% endif %}
                
                {% if celery_status.missing_tasks %}
                <div class="mb-3">
                    <strong>未注册的任务:</strong>
                    <ul>
                        {% for task in celery_status.missing_tasks %}
                        <li>{{ task }}</li>
                        {% endfor %}
                    </ul>
                </div>
                {% endif %}
            {% else %}
                <div class="alert alert-warning">无法获取Celery状态</div>
            {% endif %}
        </div>
        <div class="card-footer">
            <form method="post" action="{% url 'sync_tasks' %}">
                {% csrf_token %}
                <button type="submit" class="btn btn-primary">同步所有任务到Celery Beat</button>
            </form>
        </div>
    </div>
    
    <!-- 定时任务列表 -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title">定时任务列表</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>测试套件</th>
                            <th>环境</th>
                            <th>调度类型</th>
                            <th>状态</th>
                            <th>上次执行</th>
                            <th>下次执行</th>
                            <th>执行次数</th>
                            <th>成功率</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for task in tasks %}
                        <tr>
                            <td>{{ task.id }}</td>
                            <td>{{ task.name }}</td>
                            <td>{{ task.test_suite.name }}</td>
                            <td>{{ task.environment.name }}</td>
                            <td>{{ task.get_schedule_type_display }}</td>
                            <td>
                                <span class="badge {% if task.status == 'active' and task.is_enabled %}bg-success{% elif task.status == 'paused' %}bg-warning{% else %}bg-danger{% endif %}">
                                    {{ task.get_status_display }}{% if not task.is_enabled %} (已禁用){% endif %}
                                </span>
                            </td>
                            <td>{{ task.last_run_time|default:"-" }}</td>
                            <td>{{ task.next_run_time|default:"-" }}</td>
                            <td>{{ task.total_runs }} (成功: {{ task.successful_runs }}, 失败: {{ task.failed_runs }})</td>
                            <td>
                                {% if task.total_runs > 0 %}
                                {{ task.success_rate|floatformat:1 }}%
                                {% else %}
                                -
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group">
                                    <form method="post" action="{% url 'run_task_now' task.id %}">
                                        {% csrf_token %}
                                        <button type="submit" class="btn btn-sm btn-primary">立即执行</button>
                                    </form>
                                    <a href="{% url 'task_execution_logs' task.id %}" class="btn btn-sm btn-info ms-1">执行日志</a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="11" class="text-center">没有定时任务</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
